﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.IO;
namespace Editor
{
    public partial class Form1 : Form
    {
        private Graphics _boardGraphicsControl;
        private Graphics _tileListGraphicsControl;
        private Board _gameBoard;
        private Grid _grid;
        private Image _tileSheetImage;
        private Bitmap _map;
        private Graphics _mapGraphicsControl;
        private TileListWindow _tileListWindow;
        private TileCreator _tileCreatorWindow;
        private TileList _tileList;
        private CompleteTile _selectedTile = new CompleteTile();
        private const int ROWS = 19;
        private const int COLUMNS = 25;
        private const int IMAGESIZE = 32;
        public Form1()
        {
            Size cellSize = new Size(IMAGESIZE, IMAGESIZE);
            InitializeComponent();
            InitGraphicsData();
            _gameBoard = new Board(ROWS, COLUMNS);
            this.DoubleBuffered = true;
            LoadTiles();
            _tileList = new TileList(ref _tileListGraphicsControl, cellSize, ref _gameBoard);
            _grid = new Grid(ref _boardGraphicsControl, ref _gameBoard, ref _tileList, cellSize, ROWS, COLUMNS);
            _tileCreatorWindow = new TileCreator(ref _tileSheetImage);
            _tileListWindow = new TileListWindow(ref _tileSheetImage);
            Assign_Events();
        }

        private void Assign_Events()
        {
            tileListBox.MouseClick += new MouseEventHandler(_tileList.TileClicked);
            tileListBox.MouseDoubleClick += new MouseEventHandler(_tileList.TileDoubleClicked);
            tileListBox.MouseMove += new MouseEventHandler(_tileList.UpdateMousePosition);
            mapBox.MouseClick += new MouseEventHandler(_grid.MapClicked);
            _tileList.EditTile += new TileList.ReturnTile(_tileCreatorWindow.EditTile);
            _tileList.CreateTile += new TileList.NullDelegate(CreateNewTile);
            tileListScrollBar.Scroll += new ScrollEventHandler(_tileList.ScrollTileList);
            _tileCreatorWindow.CreatedNewTile += new TileCreator.ReturnTile(_tileList.AddTile);
            _tileCreatorWindow.UpdatedTile += new TileCreator.NulLDelegate(_tileList.UpdateTile);
            _tileCreatorWindow.Shown += new EventHandler(tileCreatorWindow_Shown);
            _tileCreatorWindow.Hidden += new TileCreator.NulLDelegate(tileCreatorWindow_Hidden);
        }

        void tileCreatorWindow_Hidden()
        {
            tileListBox.Enabled = true;
            tileListScrollBar.Enabled = true;
            mapBox.Enabled = true;
        }

        void tileCreatorWindow_Shown(object sender, EventArgs e)
        {
            tileListBox.Enabled = false;
            tileListScrollBar.Enabled = false;
            mapBox.Enabled = false;
        }

        private void InitGraphicsData()
        {
            _tileListGraphicsControl = tileListBox.CreateGraphics();
            _boardGraphicsControl = mapBox.CreateGraphics();
            _map = new Bitmap(COLUMNS * IMAGESIZE, ROWS * IMAGESIZE);
            _mapGraphicsControl = Graphics.FromImage(_map);
            _tileListGraphicsControl.Clear(Color.Black);
            _boardGraphicsControl.Clear(Color.Black);
        }

        private void LoadTiles()
        {
            Stream fileStream = this.GetType().Assembly.GetManifestResourceStream("Editor.Resources.dngn.jpg");
            _tileSheetImage = Image.FromStream(fileStream);
            fileStream.Close();
        }

        private void MouseOverDrawingBox(object sender, MouseEventArgs e)
        {
            _grid.UpdateMousePosition(e.Location);
            if (e.Button == MouseButtons.Left)
            {
                _grid.AssignTile(e.Location);
            }
        }

        private void tilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                _tileListWindow.Show();
            }
            catch
            {
                _tileListWindow = new TileListWindow(ref _tileSheetImage);
                _tileListWindow.Show();
            }
        }

        private void createNewTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateNewTile();
        }

        private void CreateNewTile()
        {
            try
            {
                _tileCreatorWindow.Show();
            }
            catch
            {
                _tileCreatorWindow = new TileCreator(ref _tileSheetImage);
                _tileCreatorWindow.Show();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (!e.Cancel)
            {
                FileStream FS = new FileStream(saveFileDialog.FileName, FileMode.Create);
                _gameBoard.Serialize(ref FS);
            }
        }
    }
}
